# -*- coding: utf-8 -*-
"""
Created on Wed Dec 02 14:30:11 2015

@author: liliangrong
"""

import urllib
import urllib2
import re

#处理页面标签类
class Tool:
    #去除img标签,7位长空格
    removeImg = re.compile('<img.*?>| {7}|')
    #删除超链接标签
    removeAddr = re.compile('<a.*?>|</a>')
    #把换行的标签换为\n
    replaceLine = re.compile('<tr>|<div>|</div>|</p>')
    #将表格制表<td>替换为\t
    replaceTD= re.compile('<td>')
    #把段落开头换为\n加空两格
    replacePara = re.compile('<p.*?>')
    #将换行符或双换行符替换为\n
    replaceBR = re.compile('<br><br>|<br>')
    #将其余标签剔除
    removeExtraTag = re.compile('<.*?>')
    def replace(self,x):
        x = re.sub(self.removeImg,"",x)
        x = re.sub(self.removeAddr,"",x)
        x = re.sub(self.replaceLine,"\n",x)
        x = re.sub(self.replaceTD,"\t",x)
        x = re.sub(self.replacePara,"\n    ",x)
        x = re.sub(self.replaceBR,"\n",x)
        x = re.sub(self.removeExtraTag,"",x)
        #strip()将前后多余内容删除
        return x.strip()


class Tieba():
    def __init__(self,base_url, see_lz, floorTag):
        self.base_url = base_url
        self.see_lz = '?see_lz=' + str(see_lz)
        self.tool = Tool()
        self.floorTag = floorTag
        
    def get_page(self,page_num):
        try:
            url = self.base_url + self.see_lz + '&pn=' + str(page_num)
            request = urllib2.Request(url)
            response = urllib2.urlopen(request)
            return response.read().decode('utf-8')
        except urllib2.URLError,e:
            if hasattr(e,"reason"):
                print u"链接百度贴吧失败,错误原因为: ",e.reason
                return None
    def getTitle(self,page):
        #page = self.get_page(1)
        pattern = re.compile(r'<h3 class="core_title_txt.*?>(.*?)</h3>',re.S)
        result = re.search(pattern,page)
        if result:
            #print result.group(1)	#test output
            return result.group(1).strip()	#delete \n\t
        else:
            return None

    def getPageNum(self,page):
        #page = self.get_page(1)
        pattern = re.compile(r'<span class="red">(.*?)</span>',re.S)
        result = re.search(pattern,page)
        if result:
            #print result.group(1)
            return result.group(1).strip()
        else:
            return None

    def getContent(self,page):
        #page = self.get_page(page)
        pattern = re.compile(r'<div id="post_content_.*?>(.*?)</div>',re.S)
        items = re.findall(pattern,page)
        #floor = 1
        contents = []
        for item in items:
            #print floor,u"楼------------------------------------------------------------------------------\n"
            content = "\n"+self.tool.replace(item)+"\n"
            contents.append(content)
            #floor += 1
        #print self.Tool.replace(items[1])
    def setFileTitle(self,title):
        if title is not None:
            self.file = open(title+'.txt','w+')
        else:
            self.file = open(file.defaultTitle+'.txt','w+')

    def writeData(self,contents):
    	for item in contents:
            if self.floorTag == "1":
                floorline = "\n"+str(self.floor)+u"------------------------------\n"
                self.file.write(floorline)
            self.file.write(item)
            self.floor += 1

    def start(self):
        indexPage = self.get_page(1)
        pageNum = self.getPageNum(indexPage)
        title = self.getTitle(indexPage)
        self.setFileTitle(title)
        if pageNum == None:
            print u"URL已失效，请重试"
            return
        try:
            print u"该帖子共有" + str(pageNum) + u"页"
            for i in range(1,int(pageNum)+1):
                print u"正在写入第" + str(i) + u"页数据"
                page = self.get_page(i)
                contents = self.getContent(page)
                self.writeData(contents)
        #出现写入异常
        except IOError,e:
            print u"写入异常，原因" + e.message
        finally:
            print u"写入任务完成"

print u"请输入帖子代号"
base_url = 'http://tieba.baidu.com/p/' + str(raw_input(u'http://tieba.baidu.com/p/'))
see_lz = raw_input("lz 1 or 0\n")
floorTag = raw_input("floor 1 or 0\n")
tieba = Tieba(base_url,see_lz,floorTag)
tieba.start()

#base_url = 'http://tieba.baidu.com/p/3138733512'
#tieba = Tieba(base_url,1)
#tieba.get_page(1)
#tieba.getTitle()
#tieba.getPageNum()
#tieba.getContent(tieba.getContent(1))